package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Code;
import com.itheima.domain.Result;
import com.itheima.domain.User;
import com.itheima.exception.BusinessException;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     */
    @GetMapping
    public Result findAll(){
        List<User> list = userService.findAll();
        return list != null && list.size() > 0 ?
                new Result(Code.QUERY_OK,list,"查询所有用户成功"):new Result(Code.QUERY_FAIL,"查询所有用户失败");
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable  int currentPage, @PathVariable int pageSize){
        PageInfo<User> info = userService.findByPage(currentPage, pageSize);
        return info != null && info.getSize()>0?
                new Result(Code.QUERY_OK,info,"分页查询成功"):new Result(Code.QUERY_FAIL,"分页查询失败");
    }

    /**
     * 根据用户id查询用户
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        User user = userService.findById(id);
        return  user != null?
                new Result(Code.QUERY_OK,user,"查询用户成功"):new Result(Code.QUERY_FAIL,"查询用户失败");
    }

    /**x
     * 保存
     * @return
     */
    @PostMapping
    public Result save(@RequestBody @Validated User user, Errors errors){
        if (errors.hasErrors()) {
            List<FieldError> fieldErrors = errors.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                String message = fieldError.getDefaultMessage();
                throw new BusinessException(message);
            }
        }
        int flag = userService.save(user);
        return flag == 1?
                new Result(Code.SAVE_OK,flag,"保存用户成功"):new Result(Code.SAVE_FAIL,"保存用户失败");

    }

    /**
     * 更具id更改信息
     * @return
     */
    @PutMapping
    public Result update(@RequestBody User user){
        int flag = userService.update(user);
        return flag == 1?
                new Result(Code.UPDATE_OK,flag,"修改用户信息成功"):new Result(Code.UPDATE_FAIL,"修改用户信息失败");

    }

    /**
     * 删除用户
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        int flag = userService.delteById(id);
        return flag == 1?
                new Result(Code.DELETE_OK,flag,"删除用户信息成功"):new Result(Code.DELETE_FAIL,"删除用户信息失败");
    }

    /**
     * 登入
     */
    @PostMapping("/login")
    public Result login(String username, String password, HttpSession session){
        User user = userService.findUserByUsernameAndPassword(username, password);
        if (user != null){
            session.setAttribute("user",user);
        }
        return user != null?
                new Result(Code.LOGIN_OK,"登入成功"):new Result(Code.LOGIN_FAIL,"登入失败");

    }
}
